$(function () {
    $('.login_form a').on('click', function () {
        $('.login_form').hide()
        $('.regester_form').show()
    })


    $('.regester_form a').on('click', function () {
        $('.regester_form').hide()
        $('.login_form').show()
    })

    // 表单验证
    layui.form.verify({
        username: [/^[a-zA-Z0-9]{1,10}$/, '用户名为1-10为数字字母'],
        psw:[/^[0-9a-zA-Z]{6,12}$/,'密码6-12位字母或数字'],
        repsw:function (value) {
            if($('#password').val()!==value) {
                return layui.layer.msg('两次密码不一致', { icon: 2 });
            }
        }

    })



    // 注册表单提交
    $('.regester_form form').on('submit', function (e) {
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/api/reguser',
            data: $(this).serialize()
        }).then(function (res) {
            if (res.data.status) {
                return layui.layer.msg(res.data.message, { icon: 2 });
            }
            layui.layer.msg(res.data.message, { icon: 1 });
            $('.regester_form form')[0].reset()
            $('.regester_form a').click()



        })
    })


    // 登录表单提交
    $('.login_form form').on('submit',function (e) {
        e.preventDefault()
        axios({
            method:'POST',
            url:'/api/login',
            data:$(this).serialize()
        }).then(function (res) {
            console.log(res.data);
            if(res.data.status) {
                return layui.layer.msg(res.data.message, {icon: 2}); 
            }

            layui.layer.msg(res.data.message, {icon: 1}); 
            localStorage.setItem('token',res.data.token)

            setTimeout(function () {
                location.href = '/index.html'
            },1000)



        })
    })


})